var Dimension = {
	addDimension:function(obj){
		// set the highest and lowest x and y values
		obj.xMax = 0;	obj.xMin = 10000000;	obj.yMax = 0; obj.yMin = 10000000;
		obj.draw = function(t){
			Game.c.save();
			Game.applyRotate(this);
			Game.c.fillStyle = this.color;
			Game.c.fillRect(-this.width/2,-this.height/2,this.width,this.height);
			Game.c.restore();
			//calculate the true center
			obj.trueX = obj.x + (obj.width /2);
			obj.trueY = obj.y + (obj.height /2);
			
			// calculate the radius of bounding circle
			// radius not necessary?
			//obj.radius = (Math.sqrt((obj.width * obj.width) + (obj.height * obj.height))) / 2;
			
			// repeat for components
			if("components" in obj){
				for(var id in obj.components){
					obj.components[id] = Dimension.centerOnParent(obj.components[id],obj);
					obj.components[id] = Dimension.calculateXOffset(obj.components[id],obj);
					obj.components[id] = Dimension.calculateYOffset(obj.components[id],obj);
					obj.components[id].draw();
				}
				// calculate corners
				obj = Dimension.calculateCorners(obj,true);
			}
		};
		
		// add true corners
		return obj;
	},
	addComponent:function(obj,xOffset,yOffset){
		var id = Game.getID();
		obj.components[id] = {
			color:"#fff",
			width:25,
			height:25,
			xOffset:xOffset,
			yOffset:yOffset
		};
		Dimension.addDimension(obj.components[id]);
		return obj;
	},
	centerOnParent:function(obj,parent){
		obj.x = parent.x;
		obj.y = parent.y;
		obj.a = parent.a;
		// center position on parent
		obj.x += (parent.width - obj.width) / 2;
		obj.y += (parent.height - obj.height) / 2;
		return obj;
	},
	calculateXOffset:function(obj,parent){
		// apply offset based on angle
		var h = obj.xOffset;
		var y = Math.sin(obj.a * Math.PI /180) * h;
		var x = Math.sqrt((h*h)-(y*y));
		if(obj.xOffset > 0){
			if(parent.a > 90 && parent.a < 270){
				x = x * -1;
			}
		} else {
			if(parent.a < 90 || parent.a > 270){
				x = x * -1;
			}
		}
		obj.x += x;
		obj.y += y;
		return obj;
	},
	calculateYOffset:function(obj,parent){
		// apply offset based on angle
		var h = obj.yOffset;
		var x = Math.sin(obj.a * Math.PI /180) * h;
		var y = Math.sqrt((h*h)-(x*x));
		if(obj.yOffset < 0){
			if(parent.a > 90 && parent.a < 270){
				y = y * -1;
			}
		} else {
			if(parent.a < 90 || parent.a > 270){
				y = y * -1;
			}
		}
		obj.x += x;
		obj.y += y;
		return obj;
	},
	calculateCorners:function(obj,draw){
		// FIND A CORNER
		// set highest and lowest x and y values
		// find the hyp
		var h = Math.sqrt((obj.width * obj.width)+(obj.height * obj.height)) /2;
		// solve for angle
		var rads = Math.atan(obj.height / obj.width);
		var a = rads / Game.radian;
		var trueA = obj.a + a;
		var trueRads = trueA * Game.radian;
		// solve for new x + y using hyp
		var y = Math.sin(trueRads) * h;
		var x = Math.sqrt((h*h)-(y*y));
		if(trueA < 270 && trueA > 90){
			x = x * -1;
		}
		obj.x1 = obj.trueX + x;
		obj.y1 = obj.trueY + y;
		// draw the corner
		if(draw){
			Game.drawPoint(obj.x1, obj.y1);
		}
		
		// solve for opposite corner
		var oppRads = trueRads + Math.PI;
		var y = Math.sin(oppRads) * h;
		var x = Math.sqrt((h*h)-(y*y));
		if(trueA > 270 || trueA < 90){
			x = x * -1;
		}
		obj.x2 = obj.trueX + x;
		obj.y2 = obj.trueY + y;
		// draw the corner
		if(draw){
			Game.drawPoint(obj.x2, obj.y2);
		}
			
		// solve for angle
		var rads = Math.atan(obj.height / (obj.width * -1));
		var a = rads / Game.radian;
		var trueA = obj.a + a;
		var trueRads = trueA * Game.radian;
		// solve for new x + y using hyp
		var y = Math.sin(trueRads) * h;
		var x = Math.sqrt((h*h)-(y*y));
		if(trueA < 270 && trueA > 90){
			x = x * -1;
		}
		obj.x3 = obj.trueX + x;
		obj.y3 = obj.trueY + y;
		// draw the corner
		if(draw){
			Game.drawPoint(obj.x3, obj.y3);
		}	
			
		// solve for opposite corner
		var oppRads = trueRads + Math.PI;
		var y = Math.sin(oppRads) * h;
		var x = Math.sqrt((h*h)-(y*y));
		if(trueA > 270 || trueA < 90){
			x = x * -1;
		}
		obj.x4 = obj.trueX + x;
		obj.y4 = obj.trueY + y;
		// draw the corner
		if(draw){
			Game.drawPoint(obj.x4, obj.y4);
		}
		
		// calculate x/y min/max
		obj.xMin = 10000000; obj.yMin = 10000000;
		obj.xMax = -10000000; obj.yMax = -10000000;
		for(var i = 1; i < 5; i++){
			if(obj['x'+i] < obj.xMin){
				obj.xMin = obj['x'+i];
			}
			if(obj['y'+i] < obj.yMin){
				obj.yMin = obj['y'+i];
			}
			if(obj['x'+i] > obj.xMax){
				obj.xMax = obj['x'+i];
			}
			if(obj['y'+i] > obj.yMax){
				obj.yMax = obj['y'+i];
			}
		}
		if(draw){
			Game.c.beginPath();
			Game.c.rect(obj.xMin,obj.yMin,obj.xMax - obj.xMin,obj.yMax - obj.yMin);
			Game.c.strokeStyle = 'red';
			Game.c.stroke();
		}
		return obj;	
	}
};